<?php TPL::output('global/header.tpl.htm'); ?>
<?php TPL::output('account/setting/setting_header.tpl.htm'); ?>

<div class="aw-mod">
	<div class="mod-body">
		<div class="aw-mod aw-user-setting-bind">
			<div class="mod-head">
				<h3><?php _e('设置交易密码'); ?></h3>
			</div>
			<form class="form-horizontal" action="account/ajax/modify_trade/" method="post" id="setting_form">
				<div class="mod-body">
					<div class="form-group">
						<label class="control-label" for="input-password-old"><?php _e('手机号码'); ?></label>
						<div class="row">
							<div class="col-lg-4" >
  								<?php if($this->vail_mobile && $this->mobile):?>
								<input type="button" class="form-control" id="mobile"  mo="<?php echo $this->mobile;?>" value="<?php echo substr_replace($this->mobile,'****',3,4);?>" style="<?php if($this->mobile):?>width: 180px;<?php endif;?>float: left;" />
								<input type="hidden" class="form-control" name="mobile" value="<?php echo $this->mobile;?>" />

  							 	<?php else:?>
								<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo $this->mobile;?>" style="width: 180px;float: left;"/>
  							 	<?php endif;?>
  								<?php if($this->mobile):?>
								<a href="account/setting/change_mobile/" class="btn btn-danger form-control"  style="float: right;width: 90px;height: 34px;background-color: #499ef3;">更换手机</a>
  							 	<?php endif;?>

							</div>
						</div>
					</div>
					<div class="aw-register-verify">
                        <div class="embed-captcha" style="width: 280px;"></div>
                        <p class="wait">正在加载验证码......</p>
                    </div>
					<div class="form-group">
						<label class="control-label" for="input-password-old"><?php _e('验证码'); ?></label>
						<div class="row">
							<div class="col-lg-4">
								<input type="text" class="form-control" id="input-password-old" name="smscode" style="width: 180px;float: left;"/>
								<input id="sendButton" type="button" value="获取验证码" class="btn btn-danger form-control" style="float: right;width: 90px;height: 34px;">
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label" for="input-password-new"><?php _e('新的交易密码'); ?></label>
						<div class="row">
							<div class="col-lg-4">
							    <input type="password" class="form-control" id="input-password-new" name="password" />
							</div>
						</div>
					</div> 
					<div class="form-group">
						<label class="control-label" for="input-password-re-new"><?php _e('确认交易密码'); ?></label>
						<div class="row">
							<div class="col-lg-4">
							    <input type="password" class="form-control" id="input-password-re-new" name="re_password" />
							</div>
						</div>
					</div>      
				</div>
			</form>
		</div>
	</div>
	<div class="mod-footer clearfix">
		<a href="javascript:;" class="btn btn-large btn-success pull-right" onclick="AWS.ajax_post($('#setting_form'));"><?php _e('保存'); ?></a>
	</div>
</div> 
<script type="text/javascript">
	var handlerEmbed = function (captchaObj) {
        $("#sendButton").click(function (e) {
            var phonereg = /^1[3456789]\d{9}$/;
            var vail_mobile="<?php echo $this->vail_mobile;?>";
            var mobile='';
		 	if(vail_mobile)
		     	mobile = $("input[name=mobile]").val();
			else
		     	mobile = $.trim($("#mobile").val());

            if(mobile=='' ){
                AWS.alert("请输入手机号");
                return false;
            }
            if(!phonereg.test(mobile)){
                AWS.alert("手机号格式不正确");
                return false;
            }
            if($('#sendButton').val()!="获取验证码"){
                return false;
            }

            var validate = captchaObj.getValidate();
            if (!validate) {
                AWS.alert("请先完成验证");
                return false;
            } else{
                $.post(G_BASE_URL + '/account/ajax/check_mobile/',{
                    mobile:mobile,
                    type:'check_mobile',
                }, function (result)
                {
                    if (result.errno == -1)
                    {
                        AWS.alert("手机号已经注册！");
                        captchaObj.reset();
                        return false;
                    }else{
                        $.ajax({
                            url: G_BASE_URL + '/tools/sendSms/',
                            type: "post",
                            dataType: "json",
                            data: {
                                mobile: mobile,
                                challenge: '',
                                geetest_challenge: validate.geetest_challenge,
                                geetest_validate: validate.geetest_validate,
                                geetest_seccode: validate.geetest_seccode,
                                client_type: 'web',
                                genre: '1'
                            },
                            success: function (result) {
                                console.log(result);
                                if(result.errno == -1)
                                {
                                    AWS.alert(result.err);
                                    captchaObj.reset(); // 调用该接口进行重置 
                                    return false;
                                }else
                                {
                                    AWS.alert(result.err);
                                    $("#sendButton").attr('disabled',"true");
					 				$("#sendButton").css('background-color',"#cec9c8").css('color','#000');
					                sendInterval = 60;
					                setInterval(function () {
					                    $("#sendButton").val(sendInterval > 0? (--sendInterval + "秒") : "获取验证码");
					                    if (sendInterval <= 0) {
					                        sendInterval = 0;
					 						$('#sendButton').removeAttr("disabled");
					 						$("#sendButton").css('background-color',"#c9302c").css('color','#fff');

					                        clearInterval();
					                    }
					                }, 1000);
                                    return true;
                                }

                            }
                        });
                    }
                }, 'json');
            }
        });
        // 将验证码加到id为captcha的元素里，同时会有三个input的值：geetest_challenge, geetest_validate, geetest_seccode
        captchaObj.appendTo(".embed-captcha");
        captchaObj.onReady(function () {
            $(".wait").css('display','none');
        });
    };

    $.ajax({
        // 获取id，challenge，success（是否启用failback）
        url: G_BASE_URL+ "/account/slide_captcha/?t=" + (new Date()).getTime()+"&client_type='web'", // 加随机数防止缓存
        type: "get",
        dataType: "json",
        success: function (data) {
            var dataObj = eval('(' + data.rsm + ')');
            // 使用initGeetest接口
            // 参数1：配置参数
            // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它做appendTo之类的事件
            initGeetest({
                gt: dataObj.gt,
                challenge: dataObj.challenge,
                new_captcha: dataObj.new_captcha,
                offline: !dataObj.success, // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                product: "embed", // 产品形式，包括：float，embed，popup。注意只对PC版验证码有效
                width: '100%' // 设置按钮宽度, 与父元素宽度一致
            }, handlerEmbed);
        }
    });
</script>


<?php TPL::output('account/setting/setting_footer.tpl.htm'); ?>
<?php TPL::output('global/footer.tpl.htm'); ?>